package com.dingwei.superiordiary.mvp.presenter;

import android.os.CountDownTimer;
import android.text.TextUtils;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.LoginBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;
import com.lihao.baselibrary.utils.RegexUtils;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.net.URL;

/**
 * Created by lihao on 2017/9/12.
 */

public class BindPhonePresenter extends basePresenter {

    private Contract.BindPhoneView bindPhoneView;

    public BindPhonePresenter(Contract.BindPhoneView view) {
        super(view);
        this.bindPhoneView = view;
    }

    public void getCode(String mobile) {
        if (TextUtils.isEmpty(mobile)) {
            showToast("请输入手机号码");
            return;
        }
        if (!RegexUtils.isMobileExact(mobile)) {
            showToast("请输入正确的手机号码");
            return;
        }

        RequestParams rp = new RequestParams(URLS.HOST + URLS.GET_VERIFY_CODE);
        rp.addBodyParameter("mobile", mobile);
        rp.addBodyParameter("type", "4");
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                MyCount myCount = new MyCount(60000, 1000);
                myCount.start();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }

    public void bindPhone(String mobile, String type, String code, String key,String nickname,String password,String surepass) {
        if (TextUtils.isEmpty(mobile)) {
            showToast("请输入手机号码");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            showToast("请输入验证码");
            return;
        }
        if (!RegexUtils.isMobileExact(mobile)) {
            showToast("请输入正确的手机号码");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        if (TextUtils.isEmpty(surepass)) {
            showToast("请确认密码");
            return;
        }

        if (!password.equals(surepass)) {
            showToast("密码不一致");
            return;
        }

        RequestParams rp = new RequestParams(URLS.HOST+ URLS.BIND_PHONE);
        rp.addBodyParameter("mobile", mobile);
        rp.addBodyParameter("type", type);
        rp.addBodyParameter("code", code);
        rp.addBodyParameter("key", key);
        rp.addBodyParameter("nickname", nickname);
        rp.addBodyParameter("password", password);
        rp.addBodyParameter("re_password", surepass);
        x.http().post(rp, new myCallback(BaseActivity.context, LoginBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                LoginBean bean = (LoginBean) baseBean;
                BaseActivity.value.setIsLogin(true);
                BaseActivity.value.setUserName(bean.data.name);
                BaseActivity.value.setToken(bean.data.sessionid);
                BaseActivity.value.setJPush(bean.data.jpush_code);
                bindPhoneView.loginSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }


    /**
     * 倒计时
     */
    public class MyCount extends CountDownTimer {


        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            bindPhoneView.CountDownTimerFinish();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            bindPhoneView.CountDownTimeronTick(millisUntilFinished);
        }

    }

}
